#include <myhead.h>
#include "controller.h"
#include "model.h"
#include "mynet.h"

extern char loginedName[20];

int friendController_list(friend_t** fArr,int* arrLen,char* errmsg){
	//返回-1发生了错误， 0-成功
	

	return 0;
}

int friendController_sendMsg(char* user,char* buf,char* errmsg){
	
	return 0;
}


int friendController_add(char* friendname, char* errmsg){
	if(strcmp(friendname, loginedName) == 0){
		strcpy(errmsg, "好友名不能和你自己的名字相同");
		return -1;
	}
	//
	char buf[48];
	pkg_packFriendAdd(buf, 0, loginedName, friendname);
	//
	spkg_ret_t ret;
	if(mynet_send_locked(buf, sizeof(buf), &ret, 2, 2, errmsg)!=0){
		//通信失败
		return -1;
	}
	strcpy(errmsg, ret.retmsg);
	return ret.ret;
}

int friendController_getAddNum(char* errmsg){
	char buf[28];
	pkg_packFriendAddNum(buf, 0, loginedName);
	//
	spkg_ret_t ret;
	int retno=mynet_send_locked(buf, sizeof(buf), &ret, 2, 2, errmsg);
	if(retno == -1){
		//通信失败
		return -1;
	}
	if(ret.ret == -1){
		strcpy(errmsg, ret.retmsg);
		return -1;
	}
	int num=0;
	memcpy(&num, ret.retmsg, sizeof(num));
	printf("num=[%d],retno=[%d],ret.ret=[%d]\n", num, retno,ret.ret);
	return ret.ret;
}
